import { observable, action, useStrict, computed, asMap, toJS } from 'mobx';
import BaseStore from './BaseStore';
import base from '../util/base';
import { _,WfForm_Public } from '../util/pcPublic';

const initState = {
    detailMark: "",
    rowIndex: -1,
    fieldArr: new Array()
}

export class EditRowStore extends BaseStore{

    @observable detailMark;
    @observable rowIndex;
    fieldArr;

    constructor(){
        super();
        this.resetState();
        this.doInitInfo = this.doInitInfo.bind(this);
        this.reEditAssignRow = this.reEditAssignRow.bind(this);
        this.generatePageInfo = this.generatePageInfo.bind(this);
        this.resetState = this.resetState.bind(this);
    }

    @action('设置编辑行信息')
    doInitInfo(queryParams={}){
        const detailMark = queryParams.detailMark;
        const rowIndex = parseInt(queryParams.rowIndex);
        this.detailMark = detailMark;
        this.rowIndex = rowIndex;
        this.generatePageInfo();
    }

    @action('重新编辑指定行')
    reEditAssignRow(rowIndex){
        if(this.rowIndex === rowIndex)
            return;
        this.rowIndex = rowIndex;
        this.generatePageInfo();
    }

    @action('计算相关信息')
    generatePageInfo(){
        this.changeState({
            fieldArr: new Array(),
            variableMap: observable.map ? observable.map({}) : asMap({})
        });
        if(!this.detailMark || this.rowIndex < 0)
            return;
        const { commonParam } = base.wfGlobalStore;
        const { dataJson,detailMap } = base.wfLayoutStore;
        const layoutObj = dataJson.etables[this.detailMark];
        const { ec,edtitleinrow,edtailinrow } = layoutObj;
        let fieldArr = ec.filter(item =>{
            const cellid = item.id;
            const rowid = parseInt(cellid.split(",")[0]);
            const etype = parseInt(item.etype);
            return etype === 3 && rowid > edtitleinrow && rowid < edtailinrow;
        });
        fieldArr.sort((a,b) =>{
            const a_cellArr = a.id.split(",");
            const a_orderid = parseInt(a_cellArr[0])*1000 + parseInt(a_cellArr[1]);
            const b_cellArr = b.id.split(",");
            const b_orderid = parseInt(b_cellArr[0])*1000 + parseInt(b_cellArr[1]);
            return a_orderid - b_orderid;
        });
        const detailObj = detailMap.get(this.detailMark);
        const { rowInfo,attr } = detailObj;
        const rowOrderArr = new Array();
        rowInfo.forEach((info,rowKey) =>{
            rowOrderArr.push(parseInt(rowKey.substring(4)));
        });
        rowOrderArr.sort((a,b) =>{
            return a - b;
        });

        const index = rowOrderArr.indexOf(this.rowIndex);
        const serialNum = index+1;
        const keyid = parseInt(rowInfo.get(`row_${this.rowIndex}`).get("keyid") || 0);
        const preRowIndex = index > 0 ? rowOrderArr[index-1] : -1;
        const nextRowIndex = index === rowOrderArr.length-1 ? -1 : rowOrderArr[index+1];
        const isViewOnly = parseInt(commonParam.isviewonly || 0);
        const isaffirmance = parseInt(commonParam.isaffirmance || 0);
        const disabledDelBtn = isViewOnly === 1 || isaffirmance === 1 || (attr.isdelete !== 1 && (attr.isadd !== 1 || keyid > 0));
        const disabledAddBtn = isViewOnly === 1 || isaffirmance === 1 || attr.isadd !== 1;
        const showCopyBtn = isViewOnly !== 1 && isaffirmance !== 1 && attr.isadd === 1 && attr.iscopy === 1;
        this.fieldArr = fieldArr;
        this.batchControlVariableMap({serialNum, preRowIndex, nextRowIndex, disabledDelBtn, disabledAddBtn, showCopyBtn});
        //执行自定义新增行接口，简单延时等组件渲染完成
        const detailIndex = parseInt(this.detailMark.substring(7));
        WfForm_Public.triggerAction(WfForm_Public.ACTION_EDITDETAILROW+detailIndex, [this.rowIndex], 100);
    }

    @action('重置状态')
    resetState(){
        this.changeState(initState);
        this.resetVariableMap();
    }

}